package com.bgds.sys.task;

import com.bgds.sys.common.constant.SocketConstant;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.module.entity.dto.AlarmInfo;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * @Description 综合看板任务(推送最近报警信息)
 * @Author lj
 * @create: 2020-09-08 15:33
 */

@Slf4j
@Component
public class SynthesizeBoardJob  implements Job {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;
    @Autowired
    private IAlarmLogService iAlarmLogService;
    @Autowired
    private IDeviceService iDeviceService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //推送最新报警信息
        AlarmInfo alarmInfo= iAlarmLogService.getMaxAlarmTime();
        if(alarmInfo!=null){
            //发送广播消息，所有订阅了/topic/broadcast路径的用户都会收到此消息
            messagingTemplate.convertAndSend(SocketConstant.BOARD_TOPIC,new ResultUtil<>().setData(alarmInfo));
        }
//
//        //推送设备监测
//        Map<String,Integer> map=new HashMap<>(10);
//        //设备总数
//        List<Device> devices= iDeviceService.listPageDevice();
//        map.put("totalCount",devices.size());
//        //异常总数
//        List<AlarmLog> abnormalLogs= iAlarmLogService.unhandleLog();
//        Integer abnormalCount=0;
//        if(CollectionUtil.isNotEmpty(abnormalLogs)){
//            Map<String,List<AlarmLog>> logMap= abnormalLogs.stream().collect(Collectors.groupingBy(log -> log.getDeviceCode() ));
//            for(String deviceCode:logMap.keySet()){
//                Optional<Device> device= devices.stream().filter(d->d.getDeviceCode().equals(deviceCode)).findAny();
//                if(device!=null){
//                    abnormalCount+=1;
//                }
//            }
//        }
//        map.put("abnormalCount",abnormalCount);
//        //正常总数
//        int normalCount= devices.size()-abnormalCount;
//        map.put("normalCount",normalCount);
//        //发送广播消息，所有订阅了/topic/broadcast路径的用户都会收到此消息
//        messagingTemplate.convertAndSend(SocketConstant.BOARD_MONITOR,new ResultUtil<>().setData(map));
    }
}
